﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XNA_TD.Wellen;

namespace XNA_TD.Menue
{
    public class SchwierigkeitsMenue : GUI
    {
        private Texture2D texture;

        public SchwierigkeitsMenue()
        {
            texture = Content.Load<Texture2D>("Menue/schwierigkeitsmenue");
            size = new Rectangle(0, 0, texture.Width, texture.Height);
            Resize();
        }

        public override void Resize()
        {
            size.X = (graphics.GraphicsDevice.Viewport.Width - size.Width) / 2;
            size.Y = (graphics.GraphicsDevice.Viewport.Height - size.Height) / 2;
        }

        public override void MouseClick()
        {
            var mouseState = Mouse.GetState();
            int gedrueckterKnopf = (int)(mouseState.Y - size.Y) * 3 / texture.Height;
            geld = 1000;
            switch (gedrueckterKnopf)
            {
                case 0:
                    schwierigkeit = Schwierigkeit.Leicht;
                    Register(new Welle(20));
                    PreBuild();
                    break;
                case 2:
                    schwierigkeit = Schwierigkeit.Schwer;
                    Register(new Welle(0));
                    break;
                default:
                    schwierigkeit = Schwierigkeit.Normal;
                    Register(new Welle(0));
                    break;
            }
            new GeldUeberZeit();
            new GeldAnzeige();
            new BauMenue();
            new GameStartMenue();
            UnRegister(this);
        }

        private void PreBuild()
        {
            geld = 1000000;
            Register(new MGTower(new Vector3(33, 0, 27)));
            Register(new MGTower(new Vector3(33, 0, 28)));
            Register(new MGTower(new Vector3(35, 0, 27)));
            Register(new MGTower(new Vector3(36, 0, 28)));
            Register(new MGTower(new Vector3(37, 0, 25)));
            Register(new MGTower(new Vector3(37, 0, 26)));
            Register(new MGTower(new Vector3(38, 0, 27)));
            Register(new MGTower(new Vector3(39, 0, 25)));
            Register(new MGTower(new Vector3(40, 0, 26)));
            Register(new MGTower(new Vector3(40, 0, 27)));
            Register(new MGTower(new Vector3(40, 0, 28)));
            Register(new MGTower(new Vector3(41, 0, 22)));
            Register(new MGTower(new Vector3(41, 0, 23)));
            Register(new MGTower(new Vector3(41, 0, 24)));
            Register(new MGTower(new Vector3(42, 0, 25)));
            Register(new MGTower(new Vector3(42, 0, 27)));
            Register(new MGTower(new Vector3(42, 0, 28)));
            Register(new MGTower(new Vector3(43, 0, 22)));
            Register(new MGTower(new Vector3(45, 0, 21)));
            Register(new MGTower(new Vector3(47, 0, 20)));
            Register(new MGTower(new Vector3(49, 0, 21)));
            Register(new MGTower(new Vector3(51, 0, 20)));
            Register(new MGTower(new Vector3(53, 0, 20)));
            Register(new MGTower(new Vector3(53, 0, 21)));
            Register(new MGTower(new Vector3(53, 0, 22)));
            Register(new MGTower(new Vector3(55, 0, 22)));
            Register(new MGTower(new Vector3(56, 0, 23)));
            Register(new MGTower(new Vector3(47, 0, 24)));
            Register(new MGTower(new Vector3(48, 0, 25)));
            Register(new MGTower(new Vector3(50, 0, 25)));
            Register(new MGTower(new Vector3(50, 0, 26)));
            Register(new MGTower(new Vector3(44, 0, 28)));
            Register(new MGTower(new Vector3(44, 0, 29)));
            Register(new MGTower(new Vector3(46, 0, 29)));
            Register(new MGTower(new Vector3(48, 0, 28)));
            Register(new MGTower(new Vector3(50, 0, 29)));
            Register(new MGTower(new Vector3(52, 0, 30)));
            Register(new MGTower(new Vector3(54, 0, 29)));
            Register(new MGTower(new Vector3(54, 0, 30)));
            Register(new MGTower(new Vector3(56, 0, 30)));
            Register(new MGTower(new Vector3(56, 0, 31)));
            
            Register(new MGTower(new Vector3(56, 0, 25)));
            Register(new MGTower(new Vector3(57, 0, 25)));
            Register(new MGTower(new Vector3(58, 0, 25)));
            Register(new MGTower(new Vector3(59, 0, 25)));
            Register(new MGTower(new Vector3(60, 0, 25)));
            Register(new MGTower(new Vector3(61, 0, 25)));
            Register(new MGTower(new Vector3(62, 0, 25)));
            Register(new MGTower(new Vector3(63, 0, 25)));
            Register(new MGTower(new Vector3(63, 0, 26)));
            Register(new MGTower(new Vector3(63, 0, 27)));
            Register(new MGTower(new Vector3(63, 0, 28)));
            Register(new MGTower(new Vector3(63, 0, 29)));
            Register(new MGTower(new Vector3(63, 0, 30)));
            
            Register(new MGTower(new Vector3(56, 0, 27)));
            Register(new MGTower(new Vector3(57, 0, 27)));
            Register(new MGTower(new Vector3(58, 0, 27)));
            Register(new MGTower(new Vector3(59, 0, 27)));
            Register(new MGTower(new Vector3(60, 0, 27)));
            Register(new MGTower(new Vector3(61, 0, 27)));

            Register(new MGTower(new Vector3(58, 0, 24)));
            Register(new MGTower(new Vector3(58, 0, 28)));
            Register(new MGTower(new Vector3(58, 0, 29)));
            Register(new MGTower(new Vector3(58, 0, 30)));
            Register(new MGTower(new Vector3(58, 0, 31)));
            
            Register(new MGTower(new Vector3(60, 0, 29)));
            Register(new MGTower(new Vector3(60, 0, 30)));
            Register(new MGTower(new Vector3(60, 0, 31)));

            int i = 0;
            foreach (var to in gameManager.getRenderList(Priority.Tower))
            {
                var t = to as Tower;
                if (t != null)
                {
                    if (i % 10 == 0)
                    {
                        t.Upgrade(t.lvlInfo.nextUpgrades.Count - 1);
                        t.Upgrade((i / 10) % (t.lvlInfo.nextUpgrades.Count));
                    }
                    if (i % 9 == 0)
                    {
                        if(t.lvlInfo.nextUpgrades.Count>0)
                            t.Upgrade( i % (t.lvlInfo.nextUpgrades.Count));
                    }
                    i++;
                }
            }
            geld = 1000;
        }

        public override void Draw()
        {
            var depthStencilState = graphics.GraphicsDevice.DepthStencilState;
            spriteBatch.Begin();
            spriteBatch.Draw(texture, size, Color.White);
            spriteBatch.End();
            graphics.GraphicsDevice.DepthStencilState = depthStencilState;
        }
    }
}
